/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/* 
 * File:   Udp.h
 * Author: ever
 *
 * Created on 2018年1月28日, 下午8:48
 */

#ifndef UDP_H
#define UDP_H
#include <string>
#include <vector>
#include <Poco/Net/DatagramSocket.h>
#include <Poco/Net/SocketAddress.h>

#include <time.h>

#include <TL_Datetime.h>
#include <TL_Common.h>
#include <TL_Thread.h>
#include <TL_Logger.h>
#include "UdpPacket.h"
#include "KKSManger.h"
#include "KafkaServer.h"
#include <map>
using namespace std;
using namespace Poco::Net;
using namespace tidp;
using pr::UdpPacket;

#define DATAGRAME_BUFFER_LENGTH 1024*1024
#define KAFKA_CACHE_MAX_SIZE 2048
//#define EDSPOINTBUFFLEN 4096

/**
#define COMPRESS_TIME_INTVAL 60*60*2 //4小时一个块
#define COMPRESS_BUFFER_LEN 2048
#define EDSPOINTBUFFLEN 4096
#define ONDAY_SECONDS (24 * 60 * 60)
#define BEIJIN_TIME (8 * 60 * 60)

extern map<string, pair<EdsPointThread*, int> > _tgn_thread_liveid;
 */



union value_t {
    int i; //值
    float f;
    unsigned int u;
};

struct myPointValue {
    time_t ts; //时间
    value_t value;
    char type = 'A';
    char quality; //类型
} __attribute__ ((packed));

struct myPointCache {
    int last_record_ts = 0;
    //u32_t kks_id;
    std::map<time_t, myPointValue> values; //time value
};

class Udp : public TL_Thread {
public:
    Udp(const string& point_conf, const string& host_and_port);
    //Udp(const Udp& orig);
    virtual ~Udp();

    //void run();

    //virtual void recordSwitchData();
    //virtual void getAllPointValues(time_t t);
    virtual void run();
    void setHistoryRootPath(const string& hrp);
    void setKafkaServer(bool iskafka, KafkaServer * kafka_svr = NULL, const string & topic = "", int partition = -1);
    void setRecordFileEnable(bool recordFileEnable);
    void record();
    void setThreadNo(int tno);
    int getThreadNo();
protected:
    map<int, myPointCache> _tgn_cache; //内存保存当前的测点数据,供实时查询
    struct timeval _t1, _t2;
    //int _intval_time; //间隔采集时间
    int _elapsed_time;
    TL_ThreadRwLock _rw_lock; //todo 可以做map lock 降低粒度
    vector<string> _kafka_cache;
private:
    DatagramSocket _datagram_socket;
    UdpPacket _udp_packet;
    char * _buffer;

    bool _is_record_file;
    bool _is_record_kafka = false;
    int _save_data_size;


    int _thread_no;


    //map<time_t, vector<int> > _timezero_liveids; //获取测点的时间为0 表示无效
    //map<int,int> _pos_liveid;
    //EdsLiveData _eds;
    //set<string> _noliveid_points; //无效测点,根本获取不到数据

    //const vector<string> _points;
    //const vector<string>& _wizids;
    //map<string,int> _wizid_liveid;
    //    LiveClient * _live_client;


    //vector<string> _kafka_cache2;
    //kafka
    KafkaServer * _kafka_svr = nullptr;
    //SwitchData * _switch_data;
    //set<SwitchData::SwitchNode*> _changed_switch_nodes;
    string _switch_topic_name;
    string _topicname;
    string _point_file;
    int _partition_num;
    int _switch_partition_num;
    //map<string, SwitchData::SwitchNode*> _switch_data_tmp_cache;
    string history_root_path;
    string history_current_path;
    time_t last_history_time_path;
    //    sqlite3 * _sqlite3_handler;
    //int _datafile_fd;
    //int _block_id_max;
};

#endif /* UDP_H */

